package com.ts.api.http.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;
import com.ts.api.module.api.entity.ApiProtocol;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *  全流程-MD5
 *
 *  @author Pan
 */
@ApiPlan(productId = "1000064", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class DemoApiPlatV3TestPhoneMd5Convert implements ApiPlatformPhoneMd5ConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("phoneMd5", apiDataExchangeParameter.getPhoneMd5());
        }));
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson);
        RespPlatformPhoneMd5DataExchange respPlatformPhoneMd5DataExchange = RespPlatformPhoneMd5DataExchange.ofSuc();

        BigDecimal price = resData.getBigDecimal("price1");
        String companyName = resData.getString("companyName");
        String productName = resData.getString("productName");

        //  用户协议
        List<ApiProtocol> listProtocol = CollUtils.newArrayList(newList -> {
            JSONArray resProtocolList = resData.getJSONArray("protocolList");
            if (ValidParam.isNotEmpty(resProtocolList)) {
                for (int i = 0; i < resProtocolList.size(); i++) {
                    JSONObject protocolObject = resProtocolList.getJSONObject(i);
                    ApiProtocol protocol = new ApiProtocol();
                    protocol.setProtocolName(protocolObject.getString("name"));
                    protocol.setProtocolUrl(protocolObject.getString("url"));
                    newList.add(protocol);
                }
            }
        });

        respPlatformPhoneMd5DataExchange.setPrice(price);
        respPlatformPhoneMd5DataExchange.setDisplayName(companyName);
        respPlatformPhoneMd5DataExchange.setProductName(productName);
        respPlatformPhoneMd5DataExchange.setProtocolList(listProtocol);
        return respPlatformPhoneMd5DataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        return JsonUtils.toJson(apiApplyParameter);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 1000)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }

        RespPlatformPhoneMd5Apply respPlatformPhoneMd5Apply = RespPlatformPhoneMd5Apply.ofSuc();
        respPlatformPhoneMd5Apply.setCallUrl(jsonObject.getString("callUrl"));
        return respPlatformPhoneMd5Apply;
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        return null;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        return Map.of();
    }
}
